package com.nbstar.film.spider.font.woff;

//import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import java.io.IOException;

/**
 * The header includes an identifying signature and indicates the specific kind
 * of font data included in the file (TrueType or CFF outline data); it also has
 * a font version number, offsets to additional data chunks, and the number of
 * entries in the table directory that immediately follows the header:
 *
 * @version 1.0.0
 * @link
 * @since 2018/6/15
 */

public class WoffHeader {

    private static final int WOFF_SIGNATURE = 0x774F4646;

    private int signature;
    private int flavor;
    private int length;
    private int numTables;
    private int reserved;
    private int totalSfntSize;
    private int majorVersion;
    private int minorVersion;
    private int metaOffset;
    private int metaLength;
    private int metaOrigLength;
    private int privOffset;
    private int privLength;

    private AbstractOTFDataStream data;

    public WoffHeader() {

    }

    public WoffHeader(AbstractOTFDataStream stream) {
        this.data = stream;
    }

    /**
     * parse woff header
     *
     * @return WoffHeader
     * @throws IOException
     */
    public WoffHeader read() throws IOException {
        signature = data.readInt();
        // 0x774F4646 woff
        if (WOFF_SIGNATURE != signature) {
            throw new RuntimeException("file is not woff");
        }
        flavor = data.readInt();
        length = data.readInt();
        numTables = data.readUnsignedShort();
        reserved = data.readUnsignedShort();
        totalSfntSize = data.readInt();
        majorVersion = data.readUnsignedShort();
        minorVersion = data.readUnsignedShort();
        metaOffset = data.readInt();
        metaLength = data.readInt();
        metaOrigLength = data.readInt();
        privOffset = data.readInt();
        privLength = data.readInt();
        return this;
    }

    public int getSignature() {
        return signature;
    }

    public void setSignature(int signature) {
        this.signature = signature;
    }

    public int getFlavor() {
        return flavor;
    }

    public void setFlavor(int flavor) {
        this.flavor = flavor;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getNumTables() {
        return numTables;
    }

    public void setNumTables(int numTables) {
        this.numTables = numTables;
    }

    public int getReserved() {
        return reserved;
    }

    public void setReserved(int reserved) {
        this.reserved = reserved;
    }

    public int getTotalSfntSize() {
        return totalSfntSize;
    }

    public void setTotalSfntSize(int totalSfntSize) {
        this.totalSfntSize = totalSfntSize;
    }

    public int getMajorVersion() {
        return majorVersion;
    }

    public void setMajorVersion(int majorVersion) {
        this.majorVersion = majorVersion;
    }

    public int getMinorVersion() {
        return minorVersion;
    }

    public void setMinorVersion(int minorVersion) {
        this.minorVersion = minorVersion;
    }

    public int getMetaOffset() {
        return metaOffset;
    }

    public void setMetaOffset(int metaOffset) {
        this.metaOffset = metaOffset;
    }

    public int getMetaLength() {
        return metaLength;
    }

    public void setMetaLength(int metaLength) {
        this.metaLength = metaLength;
    }

    public int getMetaOrigLength() {
        return metaOrigLength;
    }

    public void setMetaOrigLength(int metaOrigLength) {
        this.metaOrigLength = metaOrigLength;
    }

    public int getPrivOffset() {
        return privOffset;
    }

    public void setPrivOffset(int privOffset) {
        this.privOffset = privOffset;
    }

    public int getPrivLength() {
        return privLength;
    }

    public void setPrivLength(int privLength) {
        this.privLength = privLength;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}
